package controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import model.*;

/**
 * Servlet implementation class UserPage
 */
public class UserPage extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserPage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("DoGet UserPage");
		if(!isLanUser(request)|!isSuperUser(request)) {
			response.sendRedirect("login");
			return;
		}		
		LoginDao lDao = new LoginDao();
		ResultSet rs = lDao.selectUsers();
		String naam;
		ArrayList<String> lijst = new ArrayList<String>();		
		try {
			while(rs.next()) {
				naam = rs.getString("username");
				lijst.add(naam);
			}
		}catch (SQLException se) {
			se.printStackTrace();
		}		
		request.setAttribute("lijst", lijst);
		RequestDispatcher r = request.getRequestDispatcher("WEB-INF/UserPage.jsp");
		r.forward(request, response);		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("DoPost UserPage");
		this.doGet(request, response);
	}

}
